import requests
from urllib.parse import quote

from bs4 import BeautifulSoup
import mysql.connector

def pa(p):

    #第几页
    print(p)
    #学校名称
    schoolName = "山东大学"

    #爬取链接
    url = "https://dpwgc-1302119999.cos.ap-guangzhou.myqcloud.com/obj/%E5%B1%B1%E4%B8%9C%E5%A4%A7%E5%AD%A6-%E5%9C%A8%E7%BA%BF%E6%8B%9B%E8%81%98.html"
    #请求头
    headers = {

    }
    #爬取数据并解析
    html = requests.get(url=url)

    html.encoding = "utf-8"

    soup = BeautifulSoup(html.text, 'lxml')

    #找到所有li标签
    dataList = soup.find_all("li")

    i=0

    for data in dataList:
        #跳过第一个非招聘信息的li标签
        if i <= 1:
            i=i+1
        else:
            createTime = data.find_all("a", class_="sy_a")[0].text
            jobTitle = data.find("a", class_="sy_a omit").text
            print(createTime + "|" + jobTitle + "|" + schoolName)
            mysql_conn(jobTitle,schoolName,createTime)

#数据库连接
def mysql_conn(job_title,school_name,create_time):
    mydb = mysql.connector.connect(
        host="127.0.0.1",
        user="root",
        passwd="",
        database="hschool_job"
    )
    mycursor = mydb.cursor()

    #插入
    sql = "INSERT INTO h_job (job_title,school_name,create_time) VALUES (%s,%s,%s)"
    val = (job_title,school_name,create_time)
    mycursor.execute(sql, val)
    mydb.commit()

if __name__ == '__main__':
    pa(str(1))
